package cn.jcet.gumall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.jcet.gumall.product.entity.ProductAttrValueEntity;
import cn.jcet.gumall.product.entity.SkuSaleAttrValueEntity;
import cn.jcet.gumall.product.service.ProductAttrValueService;
import cn.jcet.gumall.product.vo.AttrRespVo;
import cn.jcet.gumall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.jcet.gumall.product.service.AttrService;
import cn.jcet.common.utils.PageUtils;
import cn.jcet.common.utils.R;



/**
 * 商品属性
 *
 * @author coder_yang
 * @email 1546656922@qq.com
 * @date 2020-06-28 20:36:22
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * /product/attr/update/{spuId}
     */
    @PostMapping("/update/{spuId}")
    public R updateSpu(@PathVariable("spuId") Long spuId,
                       @RequestBody List<ProductAttrValueEntity> entities){

        this.productAttrValueService.updateSpu(spuId,entities);
        return R.ok();
    }


    /**
     * /product/attr/base/listforspu/{spuId}
     */
    @GetMapping("listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> entities =   this.productAttrValueService.baseAttrListforspu(spuId);

        return R.ok().put("data",entities);
    }

    /**
     * 列表
     * product/attr/sale/list/{catelogId}
     */
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType")String attrType){
        PageUtils page = attrService.queryPage(params,catelogId,attrType);

        return R.ok().put("page", page);
    }
    @RequestMapping("/list")
    public R baseAttrList(@RequestParam Map<String, Object> params){
//        PageUtils page = attrService.queryPage(params,cateLogId);
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attr =  attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
//		attrService.updateById(attr);

        attrService.updateAttrById(attr);


        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
